package org.hit.burkun.calc;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.ansj.domain.Term;
import org.ansj.splitWord.analysis.ToAnalysis;
import org.apache.commons.lang3.StringUtils;
import org.hit.burkun.db.DBHelper;
import org.hit.burkun.db.DBTypeConverter;
import org.hit.burkun.model.Content;
/**
 * 对微博进行分词，并保存
 * @author burkun
 *
 */
public class Fenci {
	public static void main(String[] args){
		//insertFenci(StaticMethods.getMid2ContentTrain());
		//insertFenci(StaticMethods.getMid2ContentPredict());
	}
	public static void insertFenci(Map<String, Content> mids){
		Set<String> keys = mids.keySet();
		DBHelper dbh = DBHelper.getServerDataDBHelper();
		DBTypeConverter.DATATYPE[] types = new DBTypeConverter.DATATYPE[5];
		DBTypeConverter typeConverter = new DBTypeConverter(types);
		
		LinkedList<String[]> res = new LinkedList<>();
		for(String key : keys){
			String[] line = new String[5];
			Content c = mids.get(key);
			List<Term> ts = null;
			try{
				ts = ToAnalysis.parse(c.getCleanStr());
			}catch(Exception e){
				System.out.println("遇到异常。。。。");
			}
			LinkedList<String> fencis = new LinkedList<>();			
			if(ts !=null){
				for(Term t : ts){
					String name = t.getRealName().trim();
					if(!name.isEmpty()){
						fencis.add(t.getRealName());
					}
				}
			}
			line[0] = key;
			line[1] = StringUtils.join(fencis, ",");
			line[2] = StringUtils.join(c.getAtSomeOne(), "|$|");
			line[3] = StringUtils.join(c.getTopics(), "|$|");
			line[4] = StringUtils.join(c.getOtherTopic(), "|$|");
			res.add(line);
			if(res.size() == 1000){
				dbh.connectionDB();
				dbh.insertBatch("mid_fenci", new String[]{"mid","fenci","at_someone","topics","other_topics"}, res, typeConverter);
				System.out.println("insert 1000!");
				res.clear();
				dbh.disconnection();
			}
		}
		if(res.size()> 0){
			dbh.connectionDB();
			dbh.insertBatch("mid_fenci", new String[]{"mid","fenci","at_someone","topics","other_topics"}, res, typeConverter);
			System.out.println("insert 1000!");
			res.clear();
			dbh.disconnection();
		}
	}
}
